package com.yjyw.tiexie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.viewpager.widget.ViewPager;

import com.yjyw.tiexie.R;
import com.yjyw.tiexie.activity.login.LoginActivity;
import com.yjyw.tiexie.adapter.VPFragmentAdapter;
import com.yjyw.tiexie.bean.ClassTypeBean;
import com.yjyw.tiexie.common.BaseFragment;
import com.yjyw.tiexie.common.net.HandlerCode;
import com.yjyw.tiexie.common.net.NetAPI;
import com.yjyw.tiexie.common.net.NetCallBack;
import com.yjyw.tiexie.util.GsonUtil;
import com.yjyw.tiexie.util.SharedPreferencesUtils;
import com.yjyw.tiexie.widget.ClassTab;
import com.yjyw.tiexie.widget.tab.Children;
import com.yjyw.tiexie.widget.tab.TabSelected;
import com.gyf.barlibrary.ImmersionBar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class ClassFragment extends BaseFragment implements NetCallBack, TabSelected {


    @BindView(R.id.vp)
    ViewPager vp;
    @BindView(R.id.my_tab)
    ClassTab myTab;
    @BindView(R.id.ll_top)
    LinearLayout llTop;
    @BindView(R.id.iv_search)
    ImageView ivSearch;
    private List<Children> childTypes = new ArrayList<>();

    List<Fragment> fragmentList = new ArrayList<>();

    private boolean isFirst = true;

    @Override
    protected int getFragmentView() {
        return R.layout.fragment_class;
    }

    @Override
    public void lazyInit(View view, Bundle savedInstanceState) {
        initView();
    }

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            if (isFirst) {
                isFirst = false;
            } else {
                ImmersionBar.with(getActivity()).init();
                ImmersionBar.with(this).titleBarMarginTop(R.id.ll_top).statusBarColor(R.color.transparent).init();
            }

        }
    }

    private void initView() {
        ImmersionBar.with(getActivity()).init();
        ImmersionBar.with(this).titleBarMarginTop(R.id.ll_top).statusBarColor(R.color.transparent).init();
        //获取所有通告分类
        getTypeList();

    }

    private void getTypeList() {
        HashMap map = new HashMap();
        map.put("default", 1);
        postJson(map, NetAPI.GET_TYPE_LIST, HandlerCode.GET_TYPE_LIST, this);
    }


    @Override
    public void onResponse(boolean flag, int requestCode, String result) throws Exception {
        switch (requestCode) {
            case HandlerCode.GET_TYPE_LIST:
                ClassTypeBean classTypeBean = (ClassTypeBean) GsonUtil.getObject(result, ClassTypeBean.class);
                if (classTypeBean.getRetcode() == 200 && classTypeBean.getData() != null) {
                    if (classTypeBean.getData().size() > 0) {
                        for (int i = 0; i < classTypeBean.getData().size(); i++) {
                            childTypes.add(new Children(classTypeBean.getData().get(i).getId(), classTypeBean.getData().get(i).getCateName()));
                        }
                        vp.setOffscreenPageLimit(classTypeBean.getData().size() - 1);
                        FragmentManager fragmentManager = getChildFragmentManager();//获得fragmentManager管理器
                        vp.setAdapter(new VPFragmentAdapter(fragmentManager, fragmentList));//给ViewPager设置适配器
                        myTab.setupWithViewPager(vp);
                        myTab.setTabSelected(this);

                        myTab.setNormalTabs("child", 0, childTypes);
                        //默认活动
                        vp.setCurrentItem(0);
                    }
                }
                break;
        }
    }

    @Override
    public void tokenTimeOut(int requestCode) {

    }

    @Override
    public void onTabSelected(int tabPosition) {

    }

    @OnClick(R.id.iv_search)
    public void onClick() {
        if (!SharedPreferencesUtils.isLogin(getActivity())) {
            //游客登录
            startActivity(new Intent(getActivity(), LoginActivity.class));
            return;
        }
        //搜索页面
    }
}
